package com.csdj.t309.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.t309.entity.GymCard;
import com.csdj.t309.service.GymCardService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
/**
 * <p>
 * 前端控制器
 * </p>
 * @since 2022-03-05
 */
@Controller
@RequestMapping("/card")
public class GymCardController {
    @Resource
    private GymCardService gymCardService;
    @GetMapping("/oracle/test")
    List<GymCard> test(){
        List<GymCard> list = gymCardService.list();
        return list;
    }


    @GetMapping("/add")
    public String index() {
        return "add";
    }

    @GetMapping("/page/{typeId}")
    public String page(@RequestParam(defaultValue = "1")int pageNum,@PathVariable int typeId,Model model ){
        Page<GymCard> page = gymCardService.getListPage(pageNum, 3,typeId);
        model.addAttribute("page", page);
        return "index";
    }
    @ResponseBody
    @GetMapping("/page/{typeId}/test")
    public Page<GymCard> pageTest(@RequestParam(defaultValue = "1") Integer pageNum,@PathVariable int typeId ){
        Page<GymCard> page = gymCardService.getListPage(pageNum, 3,typeId);
        return page;
    }
    @GetMapping("/list/{typeId}")
    public String getListByTypeId(Model model,@PathVariable int typeId) {
        List<GymCard> list = gymCardService.getListByTypeId(typeId);
        model.addAttribute("list", list);
        return "index";
    }
    @PutMapping("/insert")
    public boolean insert(Integer cardTypeId,@RequestParam String userName
            ,Integer status,String phone , String balance, String openDate){
        GymCard gymCard = new GymCard();
        gymCard.setCardTypeId(cardTypeId);
        System.out.println("userName="+userName);
        gymCard.setUserName(userName);
        gymCard.setStatus(status);
        gymCard.setPhone(phone);
        gymCard.setBalance(balance);
        gymCard.setOpenDate(LocalDate.parse(openDate));

        return gymCardService.save(gymCard);
    }


    @ResponseBody
    @GetMapping("/list")
    public List<GymCard> list(@RequestParam(required = false,defaultValue = "0")Integer typeId) {
        List<GymCard> list = gymCardService.getListByTypeId(typeId);
        return list;
    }

//    @PutMapping("/insert")
//    public boolean insert(@RequestBody GymCard gymCard){
//        return gymCardService.
//    }


    @ResponseBody
    @GetMapping("/update/status")
    public String updateStatus(int status, int id) {
        int jg = gymCardService.updateStatus(status, id);
        if (jg > 0) {
            return "true";
        }
        return "false";
    }


    @ResponseBody
    @GetMapping("/type/list")
    public List<GymCard> gymCardTypeList(int Type) {
        List<GymCard> list=gymCardService.getListByTypeId(Type);
        if(list!=null){
            return list;
        }
        return null;
    }

}
